package cn.xenosp.server.config;

import cn.xenosp.server.common.cache.Cache;
import cn.xenosp.server.common.handler.ModelUpdateHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 项目名： nginx-web
 * 包路径： cn.xenosp.nginx.config
 * 作者：   TongHui
 * 创建时间: 2024-07-26 13:41
 * 描述: TODO
 * 版本: 1.0
 */
@Configuration
@Slf4j
public class WebResourcesConfig implements WebMvcConfigurer {

    @Autowired
    Cache redisCache;
    @Value("${app.config.api-base-url}")
    String apiBaseUrl;


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 加载静态资源，访问路径不需要加前缀 http://127.0.0.1:8080/1.jpg
        registry.addResourceHandler("/**",apiBaseUrl+"/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/META-INF/resources/");
        // 挂在本地目录，访问路径需要加前缀 http://127.0.0.1:8080/image/1.jpg
//        registry.addResourceHandler("/image/**").addResourceLocations("file:C:/SoftWare/Wallpaper/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("============================初始化拦截器===============================");
        ModelUpdateHandler modelUpdateHandler = new ModelUpdateHandler();
        modelUpdateHandler.setRedisCache(redisCache);
        registry.addInterceptor(modelUpdateHandler)
                .addPathPatterns("/**")
                .excludePathPatterns("POST:/login")
                // 拦截GET请求的/login接口
                .addPathPatterns("GET:/login");
    }
}
